﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_StallDetails : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    StallDetailsBLL c = new StallDetailsBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            txtStallID.Value = Page.RouteData.Values["id"].ToString();
        }
        catch
        {
            if (txtStallID.Value.Equals(""))
                txtStallID.Value = "0";
        }

        if (!IsPostBack)
        {
            loadData(int.Parse(txtStallID.Value));
            LoadDDL_Products();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }
    private void loadData(int ID)
    {
        txtStallID.Value = ID.ToString();
        var danhsach = from sd in db.stalldetails
                       join p in db.products
                       on sd.productid equals p.id
                       where sd.stallid == ID
                       select new
                       {
                           id = sd.id,
                           productid = sd.productid,
                           amount = sd.amount,
                           status = sd.status,
                           productname = p.name
                       };
        gvStalls.DataSource = danhsach;
        gvStalls.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        stalldetail cat = new stalldetail
        {
            stallid = int.Parse(txtStallID.Value),
            productid = int.Parse(ddlProductID.SelectedItem.Value),
            amount = int.Parse(txtAmount.Text),
            status = status
        };
        c.StallDetails_Insert(cat);
        this.loadData(int.Parse(txtStallID.Value));
        this.reset();
    }
    public void reset()
    {
        txtID.Value = String.Empty;
        txtStallID.Value = String.Empty;
        ddlProductID.SelectedIndex = 0;
        txtAmount.Text = String.Empty;
        rbtnStatus.SelectedIndex = 0;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void gvStalls_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtID.Value = Server.HtmlDecode(gvStalls.Rows[e.NewSelectedIndex].Cells[0].Text);
        txtStallID.Value = Server.HtmlDecode(gvStalls.Rows[e.NewSelectedIndex].Cells[1].Text);
        ddlProductID.SelectedValue = Server.HtmlDecode(gvStalls.Rows[e.NewSelectedIndex].Cells[1].Text);
        txtAmount.Text = Server.HtmlDecode(gvStalls.Rows[e.NewSelectedIndex].Cells[3].Text);
        if (gvStalls.Rows[e.NewSelectedIndex].Cells[4].Text == "False")
        {
            rbtnStatus.SelectedIndex = 1;
        }
        else
        {
            rbtnStatus.SelectedIndex = 0;
        }
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        stalldetail cat = new stalldetail
        {
            id = int.Parse(txtID.Value),
            stallid = int.Parse(txtStallID.Value),
            productid = int.Parse(ddlProductID.SelectedItem.Value),
            amount = int.Parse(txtAmount.Text),
            status = status
        };
        c.StallDetails_Insert(cat);
        this.loadData(int.Parse(txtStallID.Value));
        this.reset();
    }
    protected void gvStalls_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string CatID = gvStalls.Rows[e.RowIndex].Cells[0].Text;
        c.StallDetails_Delete(int.Parse(CatID));
        this.loadData(int.Parse(txtStallID.Value));
    }
    protected void gvStalls_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvStalls.PageIndex = e.NewPageIndex;
        this.loadData(int.Parse(txtStallID.Value));
    }

    #region Load 
    /// <summary>
    /// Hàm lấy dữ liệu các bảng category,supplier theo catid, origins lên dropdownlist
    /// </summary>
    void LoadDDL_Products()
    {
        ddlProductID.DataSource = db.products;
        ddlProductID.DataTextField = "name";
        ddlProductID.DataValueField = "id";
        ddlProductID.DataBind();
    }
    #endregion
}